package com.android.videotest.video.listvideo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.videotest.R;
import com.android.videotest.video.VideoActivity;
import com.android.videotest.video.data.MediaBean;
import com.bumptech.glide.Glide;

import java.util.List;

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VideoItemHolder> {

    private List<MediaBean> data;

    public void setData(List<MediaBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public VideoItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_video_list, parent, false);
        return new VideoItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoItemHolder holder, int position) {
        MediaBean bean = data.get(position);
        Glide.with(holder.itemView.getContext()).load(bean.getThumbPath()).into(holder.ivThumb);
        holder.tvTitle.setText(bean.getDisplayName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VideoActivity.start(holder.itemView.getContext(), bean.getPath());
            }
        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    static class VideoItemHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ImageView ivThumb;
        public VideoItemHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            ivThumb = itemView.findViewById(R.id.iv_thumb);
        }
    }

}
